package com.example.iot.controller;

import com.example.iot.entity.ArchivesPatchUpgrade;
import com.example.iot.entity.DeviceArchives;
import com.example.iot.service.impl.DeviceArchivesServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/devicearchives")
@RequiredArgsConstructor
@Component
public class DeviceArchivesController {
    private final DeviceArchivesServiceImpl deviceArchivesService;

    @GetMapping("/selectall")
    public List<ArchivesPatchUpgrade> selectAll() {
        List<ArchivesPatchUpgrade> list = deviceArchivesService.selectAll();
        return list;
    }

    @GetMapping("/resetpassword")
    public String resetPassword(int id) {
        DeviceArchives deviceArchives = new DeviceArchives();
        String password = getRandom();
        deviceArchives.setPassword(password);
        deviceArchives.setId(id);
        deviceArchivesService.updateById(deviceArchives);
        return password;
    }

    @GetMapping("/deviceverify")
    public DeviceArchives deviceVerify(String deviceId, String password) {
        DeviceArchives deviceArchives = deviceArchivesService.deviceVerify(deviceId, password);
        if (deviceArchives != null) {
            return deviceArchives;
        }
        return null;
    }

    @PostMapping("/updatedevice")
    public String updateDevice(@RequestBody DeviceArchives deviceArchives) {
        DeviceArchives deviceArchives1 = deviceArchivesService.selectByDevId(deviceArchives.getDeviceid());
        if(deviceArchives1!=null){
            deviceArchives.setId(deviceArchives1.getId());
            deviceArchivesService.updateById(deviceArchives);
            return "200";
        }
        return  "500";
    }

    public String getRandom() {
        String charPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz|<>?/(){}[]";
        StringBuilder str = new StringBuilder();
        Random rnd = new Random();
        while (str.length() < 24) { // length of the random string.
            int index = (int) (rnd.nextFloat() * charPool.length());
            str.append(charPool.charAt(index));
        }
        String finalString = str.toString();
        return finalString;
    }
    

    Map<String, LocalDateTime > deviceid=new HashMap();

    @GetMapping("/isonline")
    public void isOnLine(String deviceId) {
        deviceid.put(deviceId, LocalDateTime.now());
    }
    @Scheduled(fixedRate = 10000)
    public void changeOnline() {
        LocalDateTime now=LocalDateTime.now();
        List<DeviceArchives> list = deviceArchivesService.getBaseMapper().selectList(null);
        for (String key : deviceid.keySet()) {
            Duration duration =Duration.between(deviceid.get(key), now);
            DeviceArchives deviceArchives = deviceArchivesService.selectByDevId(key);
            if (deviceArchives == null){
                continue;
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getDeviceid().equals(key)){
                    list.remove(i);
                    break;
                }
            }
            DeviceArchives deviceArchives1=new DeviceArchives();
            deviceArchives1.setId(deviceArchives.getId());
            if(duration.getSeconds()>20){
                deviceArchives1.setState("离线");
            }else{
                deviceArchives1.setState("在线");
            }
            deviceArchivesService.updateById(deviceArchives1);
        }
        for (DeviceArchives item:
                list) {
            DeviceArchives deviceArchives1=new DeviceArchives();
            deviceArchives1.setId(item.getId());
            deviceArchives1.setState("离线");
            deviceArchivesService.updateById(deviceArchives1);

        }

    }


}
